<?php
/**
 * PHP Framework UI EntityGrid class
 * This class represents a generic data grid rendering a list of entities
 *
 * @author Andrew <andrew@vitche.com>
 * @author Frozen Rain <frozenrain@mail.ru>
 **/
class EntityGrid extends Control {

    /**
     * @var array Stores an array of entities which are rendered by the grid
     * @access private
     **/
    private $_entities = array();

    /**
     * Constructs an entity grid object
     * Allows setting grid type metadata which define grid appearance
     * and behavior
     *
     * @param <type> $arEntities
     * @param <type> $type
     **/
    public function __construct($arEntities) {
        $this->_entities = $arEntities;
        $this->type = new EntityGridType($this);
        if (count($arEntities)) {
            foreach ($arEntities as $value) {
                $this->type->setClass(get_class($value));
                break;
            }
        }
    }

    public function bind() {
        
    }

    public function process() {
    }

    public function render() {
        $str = "<table>\r\n";
        $str .= $this->renderActions();
        $str .= $this->renderHeader();
        foreach ($this->_entities as $Entity) {
            $str .= $this->renderRow($Entity);
        }
        $str .= "</table>\r\n";
        return $str;
    }

    private function renderActions() {
        $arActions = $this->type->getActions();
        if (count($arActions)) {
            $str = "<tr>\r\n";
            foreach ($arActions as $value) {
                $str .= "<th>\r\n";
                $str .= $this->renderAction($value);
                $str .= "</th>\r\n";
            }
            $str .= "</tr>\r\n";
            return $str;
        }
        return "";
    }

    // TODO: Move this code to the proper action types
    protected function renderAction($action) {
        return '<a href=\'#\' onclick=\'' . $action->toString() . '\'>' . $action->getName() . '</a>';
    }

    protected function renderCell($Entity, $Field) {
        return $Entity->$Field;
    }

    private function renderHeader() {
        $str = "<tr>\r\n";
        if (isset($this->_entities[0])) {
            $arCaptions = $this->type->getCaptions();
            if (!count($arCaptions)) {
                $arCaptions = $this->type->getFieldNames();
            }
            foreach ($arCaptions as $value) {
                $str .= "<th>\r\n" . $value . "</th>\r\n";
            }
            $arItemActions = $this->type->getItemActions();
            if (count($arItemActions)) {
                foreach ($arItemActions as $value) {
                    $str .= "<th>\r\n&nbsp;\r\n</th>\r\n";
                }
            }
        }
        $str .= "</tr>\r\n";
        return $str;
    }

    private function renderRow($Entity) {
        $str = "<tr>\r\n";

        $type = $Entity->type;
        $arFieldNames = $this->type->getFieldNames();
        foreach ($arFieldNames as $value) {
            $str .= "<td>\r\n" . $this->renderCell($Entity, $value) . "</td>\r\n";
        }

        $arItemActions = $this->type->getItemActions();
        if (count($arItemActions)) {
            foreach ($arItemActions as $value) {
                $strKeyField = $value->getKeyField();
                $str .= "<td>\r\n";
                $str .= $this->renderItemAction($value, $Entity->$strKeyField);
                $str .= "</td>\r\n";
            }
        }

        $str .= "</tr>\r\n";

        return $str;
    }

    protected function renderItemAction($action, $key) {
        return '<a href=\'#\' onclick=\'' . $action->toString($key) . '\'>' . $action->getName() . '</a>';
    }
}
?>